<?php
require_once '../src/cxss.php';
@header('Content-type: text/plain');
//ini_set('xdebug.overload_var_dump', false);

$BUFFER = '';
$UID = uniqid('test');
$_I_ = 0;
$PAGETITLE = 'Example Set';

function start ($s) {
    global $PAGETITLE, $_I_;
    $PAGETITLE = $s;
    $_I_ = 0;
}

function test ($_msg_, $_test_, $_return_ = array('test'))
{   global $UID, $BUFFER, $_I_;
    if ($_I_++) {
        $BUFFER .= ob_get_contents()."@endverbatim\r\n";
        ob_end_flush();
    }

    foreach ($_return_ as $_var_) global $$_var_;
    extract($GLOBALS, EXTR_SKIP|EXTR_REFS);

    $BUFFER .= <<<END
@section $UID-$_I_ Example $_I_
$_msg_
@code
$_test_
@endcode
@verbatim

END;

    echo <<<END

Test #$_I_. $_msg_
$_test_

END;

    ob_start();
    try {
        eval("var_dump($_test_);");
    }
    catch (Exception $_e_) {
        ExError::write($_e_);
    }
    echo "\r\n";
}

function result ($doc) {
    global $UID, $BUFFER, $PAGETITLE;
    $source = $doc->asXML();

    $BUFFER .= ob_get_contents().<<<END
@endverbatim
\b Result:
@verbatim
$source
@endverbatim

END;
    ob_end_flush();
    source($doc);

    $script = pathinfo($_SERVER['SCRIPT_FILENAME']);
    $dirname = realpath("$script[dirname]\\doc");
    if (!file_exists ($dirname)) mkdir($dirname);
    $filename = "$dirname\\$script[filename].dox";
    global $uid;
    if (false !== @file_put_contents($filename, <<<END
/**
@page $UID $PAGETITLE
$BUFFER
<i>See: $script[basename]</i>
*/
END
    )) echo "\r\noutput saved into $filename.\r\n";
}

function source ($element) {
    echo <<<END

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Result:
{$element->asXML()}
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

END;
}

function if_set ($arg) {
    if (isset($arg)) {
        //echo "".cxss::typename($arg).":\r\n"
        return true;
    }
    else {
        echo "*** NULL returned ***\r\n";
        return false;
    }
}

function nz ($arg) {
    if (is_array($arg) || is_object($arg)) {
        echo "For each ".cxss::typename($arg).":\r\n----------------------\r\n";
        return $arg;
    }
    else {
        echo "*** ".gettype($arg)." returned ***\r\n";
        return array();
    }
}
